package org.opu.pc.domain.dao.impl;

import java.util.List;

import org.hibernate.FetchMode;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.opu.pc.domain.dao.PublicationDao;
import org.opu.pc.domain.domains.Publication;
import org.opu.pc.domain.domains.Publisher;

public class PublicationDaoImpl extends AbstractHibernateDao<Integer, Publication>
        implements PublicationDao {

    @Override
    protected Class<Publication> getDomainClass() {
        return Publication.class;
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Publication> getPublicationsByName(String name) {
        DetachedCriteria criteria = DetachedCriteria.forClass(getDomainClass());
        criteria.add(Restrictions.eq("name", name));
        List<Publication> list = findByCriteria(criteria);
        // hibernateTemplate.findByCriteria(criteria);
        flush();
        return list;
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Publication> searchPublicationsByName(String name, Boolean wasApplied) {
        DetachedCriteria criteria = DetachedCriteria.forClass(getDomainClass());
        criteria.add(Restrictions.like("name", name));
        if (wasApplied != null) {
            criteria.add(Restrictions.eq("wasApplied", wasApplied));
        }
        List<Publication> list = findByCriteria(criteria);
        // hibernateTemplate.findByCriteria(criteria);
        flush();
        return list;
    }

    @Override
    public List<Publication> getPublicationsForPublisher(Publisher publicator,
            Boolean wasApplied) {
        return getPublicationsForPublisher(publicator.getId(), wasApplied);
    }

    @Override
    @SuppressWarnings("unchecked")
    public List<Publication> getPublicationsForPublisher(Integer publicatorId,
            Boolean wasApplied) {
        DetachedCriteria criterial = DetachedCriteria.forClass(getDomainClass());
        if (wasApplied != null) {
            criterial.add(Restrictions.eq("wasApplied", wasApplied));
        }
        criterial.createAlias("publishers", "publisher")
                .setFetchMode("publishers", FetchMode.JOIN);
        criterial.add(Restrictions.eq("publisher.id", publicatorId));
        
        List<Publication> list = findByCriteria(criterial);
        //hibernateTemplate.findByCriteria(criterial);
        
        flush();
        return list;
    }

//    ////
//
//    @Override
//    public Publication get(final Integer id) {
//    	return doInTransaction(new HibernateCallback<Publication>() {
//    		@Override
//    		public Publication doInHibernate(Session arg0) {
//    			return (Publication) arg0.get(getDomainClass(), id);
//    		}
//		});
//    }
//
//    @Override
//    public List<Publication> getList() {
//    	return doInTransaction(new HibernateCallback<List<Publication>>() {
//    		@SuppressWarnings("unchecked")
//			@Override
//    		public List<Publication> doInHibernate(Session session) {
//    			return session.createCriteria(getDomainClass()).list();
//    		}
//		});
//    }
//
//	public Integer save(final Publication object) {
//        return doInTransaction(new HibernateCallback<Integer>() {
//        	@Override
//        	public Integer doInHibernate(Session arg0) {
//        		Integer save = (Integer) arg0.save(object);
//        		object.setId(save);
//        		return save;
//        	}
//		});
//    }
//
//	@Override
//	public void saveOrUpdate(final Publication object) {
//		doInTransaction(new HibernateCallback<Integer>() {
//        	@Override
//        	public Integer doInHibernate(Session arg0) {
//                // arg0.saveOrUpdate(object);
//        		arg0.merge(object);
//        		return 0;
//        	}
//		});
//    }
//
//	@Override
//	public void delete(final Publication object) {
//		doInTransaction(new HibernateCallback<Integer>() {
//        	@Override
//        	public Integer doInHibernate(Session arg0) {
//        		arg0.delete(object);
//        		return 0;
//        	}
//		});
//    }

}
